﻿using DBContext;
using EV.EventBus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.Stations;
using ZoneLine.ViewModels;

namespace ZoneLine.Views.HStationViews
{
    /// <summary>
    /// HMainView.xaml 的交互逻辑
    /// </summary>
    public partial class HMainView : Window
    {
        public HMainView()
        {
            InitializeComponent();
            this.Loaded += HMainView_Loaded;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            var mbox = HandyControl.Controls.MessageBox.Ask("确定是否关闭MES系统软件？", "温馨提示");
            if (mbox != MessageBoxResult.OK)
            {
                e.Cancel = true;
            }
        }

        private void HMainView_Loaded(object sender, RoutedEventArgs e)
        {
            MessageCenter.Subscribe(nameof(HOP010Station) + "uilog", nameof(HMainView), (s) =>
            {
                HOp010ShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe(nameof(HOP040Station) + "uilog", nameof(HMainView), (s) =>
            {
                HOp040ShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });
        }

        private int _hOp010Count = 0;

        private void HOp010ShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke((Action)(() =>
                {
                    this._hOp010Count++;
                    if (this._hOp010Count > 500)
                    {
                        HOP010Log.Document.Blocks.Clear();
                        this._hOp010Count = 0;
                    }
                    HOP010Log.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    HOP010Log.ScrollToEnd();
                }));
            }
        }

        private int _hOp040Count = 0;

        private void HOp040ShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _hOp040Count++;
                    if (_hOp040Count > 500)
                    {
                        HOP040Log.Document.Blocks.Clear();
                        _hOp040Count = 0;
                    }
                    HOP040Log.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    HOP040Log.ScrollToEnd();
                });
            }
        }

        private void OpenHistoryView(object sender, RoutedEventArgs e)
        {
            var view = new HistoryData();
            view.Owner = this;
            view.Show();
        }

        private void QueryStationResultView(object sender, RoutedEventArgs e)
        {
            var view = StationResultView.GetView();
            view.Show();
        }

        private void BtnOpenConfigView(object sender, RoutedEventArgs e)
        {
            var view = new HStationConfigView();
            view.Owner = this;
            view.ShowDialog();
        }

        private void PePieceReplaceView(object sender, RoutedEventArgs e)
        {
            var view = PackageReplaceView.GetView();
            view.Show();
            view.Activate();
            //view.Topmost = true;
        }

        private void ClearDataBtn(object sender, RoutedEventArgs e)
        {
            try
            {
                InitHelper.DeleteTodayDatas();
                MessageBox.Show("今日数据清除完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除数据异常" + ex.Message);
            }
        }

        private void OpenAuth(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is HOpMainViewModel vm)
            {
                var view = new AuthView(() => { vm.EnterAuth = true; });
                view.Topmost = true;
                view.Show();
            }
        }

        private void CloseAuth(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is HOpMainViewModel vm)
            {
                vm.EnterAuth = false;
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Console.WriteLine(sender);
        }

        private void WorkSituationView(object sender, RoutedEventArgs e)
        {
            AuthView authView = new AuthView(() => {
                WorkSituationView workSituationView = new WorkSituationView();
                workSituationView.Show();
            });
            authView.ShowDialog();
        }
    }
}